﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Vote
{
    public partial class VoteAdd : System.Web.UI.Page
    {
        int userID;// AppCode.TestUserInfo._id;//暂用用户ID，写死，到时候用COOKIE替换过来
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;
            string btnSubmit = Request.Form["btnSubmit"];
            if (!string.IsNullOrEmpty(btnSubmit))
            {
                //提交
                msg =SubmitVote(msg);
            }
            StringBuilder sbHtml = new StringBuilder(Cache["user_voteadd"].ToString());//主sb
            #region 页面处理
            //公共部分
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            sbHtml = sbHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion
            Response.Write(sbHtml.ToString() + msg);//最终写出整个Html页面
        }


        private string SubmitVote(string msg)
        {
            Jy123.Model.User_Vote mUserVote = new Jy123.Model.User_Vote();
            Jy123.BLL.User_Vote bllUserVote = new Jy123.BLL.User_Vote();

            Jy123.Model.User_VoteOption mVoteOption = new Jy123.Model.User_VoteOption();
            Jy123.BLL.User_VoteOption bllVoteOption = new Jy123.BLL.User_VoteOption();

            //投票标题
            string txtTitle = Request.Form["txtTitle"];
            if (string.IsNullOrEmpty(txtTitle)) { msg = "<script>$(function(){$.moonbox.warning('标题不能为空!');})</script>"; }
            else { mUserVote.Title = txtTitle; }
            //投票描述
            string txtIntro = Request.Form["txtIntro"];
            if (!string.IsNullOrEmpty(txtIntro))
            { mUserVote.Intro = txtIntro; }

            //检查选项是否为空
            int optionValueCount = 0;
            for (int i = 0; i < 20; i++)
            {
                string option = Request.Form["option_" + i];
                if (!string.IsNullOrEmpty(option))
                {
                    optionValueCount += 1;
                }
            }
            if (optionValueCount >= 2)
            {
                mUserVote.FUser = userID;
                mUserVote.AddTime = DateTime.Now;
                mUserVote.Mode = Convert.ToInt32(Request.Form["Mode"]);
                string strEndTime = Request.Form["isEnd"];
                if (strEndTime == "on") { mUserVote.EndTime = DateTime.MaxValue; }
                mUserVote.JoinCount = 0;
                mUserVote.VoteCount = 0;
                mUserVote.CommentCount = 0;
                string strIsFriend = Request.Form["IsFriend"];
                if (strIsFriend == "on") { mUserVote.IsFriend = 1; }
                int newVoteID = bllUserVote.Add(mUserVote);//插入一个新投票记录,返回投票的ID
                //加入动态
                Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                mUserVote = bllUserVote.GetModel(newVoteID);
                new Jy123.BLL.Public.State().CreateVoteState(mUserForState,null,mUserVote,(int)Jy123.BLL.Public.EnumConfig.StateActionType.CreateVote);
                //加入动态
                //下面执行插入N个投票选项
                mVoteOption.FVote = newVoteID;
                mVoteOption.Count = 0;

                for (int i = 0; i < 20; i++)
                {
                    string option = Request.Form["option_" + i];
                    if (!string.IsNullOrEmpty(option))
                    {
                        mVoteOption.OptionName = option;
                        bllVoteOption.Add(mVoteOption);
                    }
                }
                msg = "<script>$(function(){$.moonbox.ok('添加成功!');})</script>";
            }
            else
            {
                msg = "<script>$(function(){$.moonbox.warning('请至少填写两个以上投票项!');})</script>";
            }
            return msg;
        }
    }
}
